﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Controls.Primitives;
using System.Threading;
using System.Windows.Threading;
using LotteryAdmin.Controls;
namespace LotteryAdmin
{
    public partial class MainPage : UserControl
    {
        static Popup _tip = new Popup();
        static DispatcherTimer timer;
        static MediaElement media;
        public MainPage()
        {
            InitializeComponent();
            this.header.Logout += new Controls.WatchHeader.LogoutHandler(header_Logout);
            this.header.ChangePwd += new Controls.WatchHeader.ChangePwdHandler(header_ChangePwd);
            CreateVodio();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(30);
            timer.Tick += new EventHandler(timer_Tick);
            timer_Tick(null, null);
            timer.Start();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            LotteryAdminClient srv = WcfProxy.GetProxy;
            CreateVodio();
            srv.GetMoneyOutListAsync();
            srv.GetMoneyOutListCompleted += (s, er) =>
            {
                if (er.Result.Count > 0)
                {
                    media.Play();
                    Notify_Show("提现通知", string.Format("有{0}笔提现申请需要您处理!", 
                        er.Result.Count), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
            };
        }
        void CreateVodio()
        {
            media = new MediaElement();
            media.Height = 0;
            media.Width = 0;
            media.Volume = 1;
            media.AutoPlay = false;
            media.Source = new Uri("msg.mp3", UriKind.Relative);
            border_Media.Child = media;
        }
        void header_ChangePwd(object sender, RoutedEventArgs e)
        {
            AdminChangePwd ac = new AdminChangePwd();
            ac.ShowWindow(App.Session["UserName"].ToString());
            ac.Closed += (s, er) =>
            {
                if (ac.DialogResult == true)
                {
                    MessageBox.Show("密码修改成功，请重新登录!");
                    this.ContentFrame.Source = new Uri("/Home", UriKind.Relative);
                    App.Navigation(new LoginPage());
                }
            };
        }

        void header_Logout(object sender, RoutedEventArgs e)
        {
            this.ContentFrame.Source = new Uri("/Home", UriKind.Relative);
            ContentFrame.Navigated -= new NavigatedEventHandler(ContentFrame_Navigated);
            App.Session.Clear();
            timer.Stop();
            App.Navigation(new LoginPage());
        }
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            if (!App.Session.ContainsKey("UserName")
                || App.Session["UserName"] == null)
            {
                this.ContentFrame.Source = new Uri("/Home", UriKind.Relative);
                App.Navigation(new LoginPage());
                return;
            }
            header.LoginName = App.Session["UserName"].ToString();
            if (this.ContentFrame.Content.GetType() == typeof(Home))
                txtbCurPosition.Text = "系统欢迎界面";
        }
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow("页面加载错误", "系统发生未知错误，造成原因可能是网络故障或者服务器太过繁忙，请稍后再试!");
            errorWin.Show();
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hlbtn = sender as HyperlinkButton;
            txtbCurPosition.Text = hlbtn.Content.ToString();
        }
        void Notify_Show(string Title, string Content, string Time)
        {
            Size size = this.LayoutRoot.RenderSize;
            MsgNotify msg = new MsgNotify();
            msg.lbTime.Text = Time;
            msg.lbTitle.Text = Title;
            msg.lbContent.Text = Content;
            _tip.Child = msg;
            msg.btnClose.MouseLeftButtonDown += (s, e) =>
            {
                _tip.Child = null;
                _tip.IsOpen = false;
                media.Stop();
            };
            _tip.IsOpen = true;
            _tip.HorizontalOffset = size.Width - 220;
            _tip.VerticalOffset = size.Height -  85;
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Size size = this.LayoutRoot.RenderSize;
            _tip.HorizontalOffset = size.Width - 220;
            _tip.VerticalOffset = size.Height - 85;
        }
    }
}